﻿Public Class frmRendezvousJour
    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing
    Private m_SortingColumn As ColumnHeader
    Private m_SortingIndex As Integer = 0

    Private Sub frmRendezvousJour_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        TE_DEBUT.Value = Today
        TE_FIN.Value = Today
        Call InitComboAgenda(CB_AGENDA)
        CB_AGENDA.SelectedIndex = CB_AGENDA.FindString("D")

    End Sub

    Private Sub InitDossiers(ByVal ix As Integer)

        Dim nb As Integer = 0

        ' extraire les RDV dans une table temporaire
        Call RDVtoTemporaire()

        ' effacer la listview
        LV_DOSSIERS.Items.Clear()
        LV_DOSSIERS.ListViewItemSorter = Nothing

        ' lire toutes les dossiers du clients
        ' lire tous les rdv extraits
        szSQL = "CALL spGET_T_IMP_RDV_JOUR('" & gUserCode & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        Do While Not rs.EOF
            LV_DOSSIERS.Items.Add(rs.Fields("datevi").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(Format(rs.Fields("heurevi").Value, "HH:mm"))
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("nom").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("adr").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("comment").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(Format(rs.Fields("no_id").Value, "000000"))
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("rubrique").Value)
            LV_DOSSIERS.Items(nb).SubItems.Add(rs.Fields("societe").Value)
            Select Case rs.Fields("societe").Value
                Case "PE"
                    LV_DOSSIERS.Items(nb).ForeColor = Color.Black
                Case "ME"
                    LV_DOSSIERS.Items(nb).ForeColor = Color.Orange
                Case "PO"
                    LV_DOSSIERS.Items(nb).ForeColor = Color.Blue
                Case "PL"
                    LV_DOSSIERS.Items(nb).ForeColor = Color.Green
            End Select
            rs.MoveNext()
            nb = nb + 1
        Loop
        rs.Close()

        ' restaurer l'ancienne ordre de tri de la listview
        If m_SortingIndex > 0 Then
            m_SortingColumn.Text = m_SortingColumn.Text.Replace("> ", "")
            SortListView(LV_DOSSIERS, m_SortingColumn, m_SortingIndex)
        End If
        ' restaurer la position du curseur avant le init
        setIndice(ix, LV_DOSSIERS)

    End Sub


    Private Sub CB_AGENDA_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles CB_AGENDA.SelectedIndexChanged
        Call InitDossiers(0)
    End Sub

    Private Sub RDVtoTemporaire()

        'Purger la Table Temporaire
        ExecQuery(ctDSN_DEVFAC, "CALL spDEL_T_TABLE('T_IMP_RDV_JOUR','" & gUserCode & "')")

        ' générer les RDV de la societe PEINTURE
        szSQL = "CALL spINS_T_IMP_RDV_JOUR('PE','" & Format(TE_DEBUT.Value, "yyyy/MM/dd") & "','" & _
                      Format(TE_FIN.Value, "yyyy/MM/dd") & "','" & CB_AGENDA.SelectedItem.ToString.Substring(0, 1) & "','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ' lire les RDV de la societe MENUISERIE
        szSQL = "CALL spINS_T_IMP_RDV_JOUR('ME','" & Format(TE_DEBUT.Value, "yyyy/MM/dd") & "','" & _
                      Format(TE_FIN.Value, "yyyy/MM/dd") & "','" & CB_AGENDA.SelectedItem.ToString.Substring(0, 1) & "','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ' lire les RDV de la societe POLYNET
        szSQL = "CALL spINS_T_IMP_RDV_JOUR('PO','" & Format(TE_DEBUT.Value, "yyyy/MM/dd") & "','" & _
                      Format(TE_FIN.Value, "yyyy/MM/dd") & "','" & CB_AGENDA.SelectedItem.ToString.Substring(0, 1) & "','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ' lire les RDV de la societe VERSANIT
        szSQL = "CALL spINS_T_IMP_RDV_JOUR('PL','" & Format(TE_DEBUT.Value, "yyyy/MM/dd") & "','" & _
                      Format(TE_FIN.Value, "yyyy/MM/dd") & "','" & CB_AGENDA.SelectedItem.ToString.Substring(0, 1) & "','" & gUserCode & "')"
        ExecQuery(ctDSN_DEVFAC, szSql)

    End Sub

End Class